package com.bdk.middle.goods.web;


import com.bdk.middle.common.domain.PageDO;
import com.bdk.middle.common.result.ResultDO;
import com.bdk.middle.common.result.web.ObjectResponse;
import com.bdk.middle.goods.api.domain.AttributeDO;
import com.bdk.middle.goods.api.query.AttributeQueryDO;
import com.bdk.middle.goods.service.AttributeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  商品属性
 * </p>
 *
 * @author Reagan
 * @since 2021-03-18
 */
@Api(tags = "商品属性管理相关接口")
@RestController
@RequestMapping("/attribute")
public class AttributeController {

    @Autowired
    private AttributeService attributeService;
    @ApiOperation(value="商品属性新增", notes="商品属性新增")
    @RequestMapping(value="/add.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse add(@RequestBody AttributeDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = attributeService.add(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }
    @ApiOperation(value="商品属性编辑", notes="商品属性编辑")
    @RequestMapping(value="/edit.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse edit(@RequestBody AttributeDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = attributeService.edit(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }
    @ApiOperation(value="商品属性删除", notes="商品属性删除")
    @RequestMapping(value="/delete.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse delete(@RequestBody AttributeDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = attributeService.remove(domain.getId(), null);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }
    @ApiOperation(value="商品属性详情", notes="商品属性详情")
    @RequestMapping(value="/detail.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse detail(@RequestBody AttributeDO domain) {

        ObjectResponse<AttributeDO> response = new ObjectResponse();

        ResultDO result = attributeService.get(domain.getId());

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((AttributeDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }
    @ApiOperation(value="商品属性列表", notes="商品属性列表")
    @RequestMapping(value="/list.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse list(@RequestBody AttributeQueryDO domain) {

        ObjectResponse<PageDO<AttributeDO>> response = new ObjectResponse();

        ResultDO result = attributeService.find(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((PageDO<AttributeDO>) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }
}

